package com.yuanfeng.base.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.base.dto.CommissionSettingVO;
import com.yuanfeng.base.entity.BaseVkSetEntity;
import com.yuanfeng.base.mapper.BaseVkSetMapper;
import com.yuanfeng.base.service.BaseVkSetService;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.DateUtils;
import com.yuanfeng.commoms.util.RedisUtil;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.excel.Constants;
import com.yuanfeng.commoms.vo.fx.GoodsVkSetVO;
import com.yuanfeng.commoms.vo.order.OrderCommissionVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @ClassName BaseVkSetServiceImpl
 * @Description 微客配置表
 * @Author cyp
 * @Date 2023-06-05 10:48:40
 * @Version 1.0
 **/
@Service
public class BaseVkSetServiceImpl extends ServiceImpl<BaseVkSetMapper, BaseVkSetEntity>
        implements BaseVkSetService {
    @Override
    public GoodsVkSetVO queryGoodsVkSet() {
        String data =  RedisUtil.get(Constants.FX_GOODS_SET);
        GoodsVkSetVO goodsVkSet;
        if(StringUtils.isBlank(data)){
            goodsVkSet  = this.baseMapper.selectByPrimaryKey(GoodsVkSetVO.VK_SET_ID);
            if(null == goodsVkSet){
                goodsVkSet = new GoodsVkSetVO(0);
            }
            RedisUtil.set(Constants.FX_GOODS_SET, JSON.toJSONString(goodsVkSet),RedisUtil.EXRP_MONTH);
        }else{
            goodsVkSet = JSON.parseObject(data,GoodsVkSetVO.class);
        }
        return goodsVkSet;
    }

    @Override
    public ResponseResult addOrUpdateCommissionSetting(CommissionSettingVO param) {
        //查询分佣设置列表
        BaseVkSetEntity result=this.baseMapper.selectOne(new LambdaQueryWrapper<BaseVkSetEntity>());
        if(null==result){
            //如果为空，就是插入新数据
            Integer results = this.baseMapper.insertCommissionSetting(param);
            if(results>=1){
                //插入成功后把redis数据删除掉
                RedisUtil.remove(Constants.FX_GOODS_SET);
            }
        }else{
            //如果不为空，就是更新新数据
            Integer results = this.baseMapper.updateCommissionSetting(param);
            if(results>=1) {
                //更新成功后，把redis数据删掉
                RedisUtil.remove(Constants.FX_GOODS_SET);
            }
        }
        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
    }

    @Override
    public ResponseResult commissionSetting() {
        //查询分佣设置列表
        BaseVkSetEntity result=this.baseMapper.selectOne(new LambdaQueryWrapper<>());
        if(null==result){
            return ResponseResult.fail(BizCodeEnume.NO_INFORMATION_OBTAINED);
        }else {
            RedisUtil.set(Constants.FX_GOODS_SET, JSON.toJSONString(result),RedisUtil.EXRP_MONTH);
            return ResponseResult.success(result);
        }
    }

    @Override
    public ResponseResult updateOrderAfterSale(String orderId) {
        OrderCommissionVO orderCommission = this.baseMapper.selectOrderCommission(orderId);
        if (null != orderCommission) {
            GoodsVkSetVO goodsVkSet = this.baseMapper.selectByPrimaryKey(GoodsVkSetVO.VK_SET_ID);
            this.baseMapper.updateOrderAfterSaleTime(orderId, DateUtils.getAfterDate(goodsVkSet.getAfterSalesDays()));
        }
        return ResponseResult.success();
    }
}
